package com.jack.service.impl;

import com.jack.mapper.DeptMapper;
import com.jack.mapper.EmpMapper;
import com.jack.pojo.Dept;
import com.jack.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 部门管理服务实现类
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    /**
     * 获取所有部门信息
     * @return
     */
    public List<Dept> getAllDepts() {
        return deptMapper.getAllDepts();
    }

    /**
     * 根据部门id删除部门
     * @param id
     */
    public void deleteDeptById(Integer id) {
        // 判断该部门下是否有员工，有员工则返回错误信息，不可删除
        Integer count = empMapper.getCountByDeptId(id);
        if (count > 0) {
            throw new RuntimeException("该部门下有员工，不能删除");
        }
        deptMapper.deleteDeptById(id);
    }


    /**
     * 新增部门
     * @param dept
     */
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.addDept(dept);
    }

    /**
     * 根据id查询部门信息
     * @param id
     * @return
     */
    public Dept getDeptById(Integer id) {
        Dept dept = deptMapper.getDeptById(id);
        return dept;
    }

    /**
     * 更新部门信息
     * @param dept
     */
    public void updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateDept(dept);
    }
}
